package com.pt.user.impl;

import java.util.List;

import com.pt.user.entites.Organization;
import com.pt.user.entites.User;

import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class UserDAOImpl extends HibernateDaoSupport implements IUserDAOImpl {

	Session session = null;

	private Session getSessions() {
		if (this.session == null)
			this.session = this.getSession();
		return this.session;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<User> getAllUser() {
		List<User> list = getSessions().createQuery("from User").list();
		return list;
	}

	@Override
	public boolean setUser(User user) {
		boolean flag = false;
		this.getSessions().save(user);
		this.getSessions().flush();
		flag = true;

		return flag;
	}

	@Override
	public User getUser(long id) {
		User user = (User) getSessions().get(User.class, id);
		return user;
	}

	@Override
	@SuppressWarnings("unchecked")
	public Organization getOrganizationByName(String name) {
		List<Organization> list = getSessions().createQuery(
				"from Organization where organization_name=?").setString(0,
				name).list();
		if (list.size() != 0) {
			return list.get(0);
		} else
			return null;

	}

}
